package com.kou.backend.handler;

import com.kou.backend.resposne.ResultResponse;
import com.kou.backend.util.JsonUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 403无权限访问处理器
 *
 * @author: Kou
 * @since: 2023/8/31 20:46
 */
@Component
public class BackendAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.setStatus(403);
        response.getWriter().write(JsonUtils.toJSONString(ResultResponse.fail("403", "没有操作权限!请联系管理员!")));
        response.getWriter().flush();
    }
}
